import axios from 'axios';
import base from '@/common/services/base';
const apiUrl = base.test;
import { getCookie,setCookie } from '@/utils/cookiesUtil.js'
import {getQuery} from '@/utils/utils'
import getOauth from '@/common/getOauth.js'



export default async function wxLogin(getData = ()=> {}) {
    if (getCookie('openId')) {
        return false;
    } else if (getQuery('code') && !getCookie('openId')){
        await axios({
			url: `${apiUrl}/wechat/getWechatUserInfo`,
            method: 'get',
            params:{
                code: getQuery('code'),
				parentId: getQuery('parentId') || ''
            },
        }).then(res => {
            if (res.data && res.data.success === true) {
                const {nickname, headimgurl, openId,  baseUserId } = res.data.result;;
                // 把token 和用户信息存在本地
                !!openId && setCookie('openId', openId);
				!!baseUserId && setCookie('baseUserId', baseUserId);
				!!nickname && setCookie('nickname', nickname);
				!!headimgurl && setCookie('headimgurl', headimgurl);
            } else {
                location.href = window.location.href.split('code=')[0].split('state=')[0];
            }
        }).catch(err => {
            console.log('err:', err);
            location.href = window.location.href.split('code=')[0].split('state=')[0];
        })
    } else {
        await getOauth();
    }
}